package dfsketches.gr3d;

public class ReanimationThread extends Thread {

	private Boolean toStop;
	private Scene scene;
	private Renderer renderer;
	private Window window;
	private Cutter cutter;
	private RenderingInfo info;
	
	public ReanimationThread(Scene sc, RenderingInfo inf, Renderer r, Cutter cut, Window w, int delay) {
		scene = sc;
		renderer = r;
		cutter = cut;
		window = w;
		info = inf;
	}

	@Override
	public void run() {
		long tSum = 0;
		long tN = 0;
		
		boolean toStopLocal = toStop = false;
		while (!toStopLocal) {
			long t = System.currentTimeMillis();
			scene.reanimate();

			if (tN == 100) {
				tN = 0;
				tSum = 0;
			}
			scene.render(renderer, cutter, info.getMonitor());
//			System.out.println(System.currentTimeMillis() - t);

			window.draw();
			System.out.println((tSum += System.currentTimeMillis() - t) / (++tN));

			synchronized (toStop) {
				toStopLocal = toStop;
			}
		}
	}
	
	public void end() {
		synchronized (toStop) {
			toStop = true;
		}
	}
	
}
